<%@ page contentType="text/html; charset=UTF-8"  %>
<%@ include file="/classes/DBHelper.class.jsp" %>
<%@ include file="/classes/CommonHelper.class.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String mid = getParameterAndSetAttribute(request,"mid");
	String chid = getParameterAndSetAttribute(request,"chid");
	String method = getParameterAndSetAttribute(request,"method");//执行操作
	String month = getParameterAndSetAttribute(request,"month");//yyyy-MM-dd
	String title = getParameterAndSetAttribute(request,"title");//yyyy-MM-dd
	String loginId = (String) (session.getAttribute("loginId") == null ? "" : session.getAttribute("loginId"));
	
	String userSql = "select login_id,user_id,a.name username,b.name deptname from cms_user a join cms_department  b on a.dept_id=b.dept_id where  a.login_id='" + loginId + "'";
	Map<String,Object> userMap = getObject(userSql);
	String deptname = "";
	String user_id = "";
	if(null!=userMap){
		deptname = (String)userMap.get("deptname");
		user_id = (String)userMap.get("user_id");
	}
	
	String filterSql = "";
	if (!"开普互联".equals(deptname) && !"信息中心".equals(deptname)) {
		filterSql += " and t1.creator_id='" + user_id + "'";
	}
	//method为1执行删除操作
	if("1".equals(method)){
		if(!nullOrEmpty(mid) && !nullOrEmpty(chid)){
			String delSql = "delete from nrbz_cy where rowid in (select max(rowid) from nrbz_cy where mid = ? and chid = ?) and mid = ? and chid = ?";
			executeUpdate(delSql,new Object[]{mid,chid,mid,chid});
		}
	}
	
	String  strSdf= "yyyy-MM";
	if(nullOrEmpty(month)){
		month = fotmatTime(new Date(),strSdf);
		pageContext.setAttribute("month",month);
	}
	
	if(!nullOrEmpty(month)){
        long time1 = 0;
        long time2 = 0;
        try{
            Date d = toDate("yyyy-MM-dd HH:mm:ss",month+"-01 00:00:00");
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            c.add(Calendar.MONTH,1);
            
            time1 = d.getTime();
            time2 = c.getTimeInMillis();
        }catch(ParseException e){
        }
		filterSql += " and t1.published_time > " +time1 + " and t1.published_time < "+time2 ;
	}
	List<Object> params = new ArrayList<Object>();
	if(!nullOrEmpty(title)){
		filterSql += " and t1.title like ?";
		params.add("%"+title+"%");
		params.add("%"+title+"%");
	}

	String sql = 
		"select tt1.*, tt2.status, tt3.un"+
		"  from (select t1.manuscript_id,"+
		"               t1.title,"+
		"               t5.dept_id,"+
		"               t5.name dept_name,"+
		"               t2.channel_id,"+
		"               t2.channel_name,"+
		"               to_char((to_date('1970-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') + t1.published_time/1000/24/60/60),'yyyy-mm-dd') published_time"+
		"          from cms_manuscript t1,"+
		"               cms_channel    t2,"+
		"               cms_website    t3,"+
		"               cms_user       t4,"+
		"               cms_department t5"+
		"         where t1.channel_id = t2.channel_id"+
		"           and t2.website_id = t3.website_id"+
		"           and t1.creator_id = t4.user_id"+
		"          and t4.dept_id = t5.dept_id"+
		"          and t3.website_id = 'e0e74680d4134e2a95875967c279e850'"+
		"          and t1.status = '4'"+
		"          and t2.status = '3'"+
		filterSql + 
		"       union all"+
		"       select t1.manuscript_id,"+
		"              t1.title,"+
		"              t5.dept_id,"+
		"              t5.name dept_name,"+
		"              t2.channel_id,"+
		"              t2.channel_name,"+
		"              to_char((to_date('1970-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss') + t1.published_time/1000/24/60/60),'yyyy-mm-dd') published_time"+
		"         from cms_manuscript  t1,"+
		"              cms_channel     t2,"+
		"              cms_website     t3,"+
		"              cms_user        t4,"+
		"              cms_department  t5,"+
		"              cms_carbon_copy t6"+
		"        where t2.website_id = t3.website_id"+
		"          and t1.creator_id = t4.user_id"+
		"          and t4.dept_id = t5.dept_id"+
		"          and t1.status = '4'"+
		"          and t2.status = '3'"+
		"          and t3.website_id = 'e0e74680d4134e2a95875967c279e850'"+
		"          and t1.manuscript_id = t6.manuscript_id"+
		"          and t6.cc_target_channel_id = t2.channel_id"+
		filterSql + 
		"          and t6.status != '5') tt1,"+
		"      nrbz_cyqk tt2,"+
		"      nrbz_cy tt3"+
		" where tt1.manuscript_id = tt2.cms_manuscript_id(+)"+
		"  and tt1.manuscript_id = tt3.mid"+
		"  and tt1.manuscript_id = tt3.mid"+
		"  and tt1.channel_id = tt3.chid"+
		" order by tt1.dept_id, tt1.published_time desc";
	
	int currentPage = 1; 
	if(!nullOrEmpty(getParameter(request,"currentPage"))){
		currentPage = Integer.parseInt(getParameter(request,"currentPage"));
	}
	//分页查询
	int pageSize = 10;
	List<Map<String,Object>> list = queryForListPage(sql,params,currentPage,pageSize);
	System.out.println(sql);
	int total = queryForPageCount(sql,params);
	
	Map<String,Integer> pageMap = new HashMap<String,Integer>();
	pageMap.put("currentPage",currentPage);
	pageMap.put("total",total);
	request.setAttribute("page",pageMap);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <style>
            body{margin:0px; padding:0px; font-size:12px; font-family:"SimSun"; color:#000; }
            a{color:#000; text-decoration:none;}
            a:hover{ text-decoration:none; color:#F00;}
        </style>
        <script type="text/javascript" src="./js/My97DatePicker/WdatePicker.js"></script>
    </head>

    <body>
        <form name="myform" action="pass_showmore.jsp?r=<%=new Date().getTime() %>" method="post" id="myform">
            <table width="100%">
                <tr>
                    <td align="center">标题：
                        <input type="text" name="title" value="${title }"/>   日期：
                        <input name="month" type="text" value="${month }" size="15" readonly="readonly" onClick="WdatePicker({dateFmt:'yyyy-MM',maxDate:'%y-%M}'})" />   <input type="submit" value="查询" /> &nbsp;&nbsp;&nbsp;&nbsp;<%if(!month.equals("")){%>该月<%}else{%>总共<%}%>报送通过<%=total%>条</td>
                </tr> 
            </table>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
               		<td width="5%" height="40" style="text-indent:20px; border-bottom:1px dotted #CCC;text-align:center;">序号</td>
                    <td width="55%" height="40" style="text-indent:20px; border-bottom:1px dotted #CCC;text-align:center;">标题</td>
                    <td width="10%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">栏目</td>
                    <td width="10%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">时间</td>
                    <td width="15%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">部门</td>
                    <td width="10%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">操作</td>
                </tr>
                <c:forEach items="<%=list %>" var="m" varStatus="i">
                <tr>
                	<td width="5%"  height="40" style="text-indent:20px; border-bottom:1px dotted #CCC;text-align:left;" >${i.index+1 }</td>
                    <td width="50%" height="40" style="text-indent:20px; border-bottom:1px dotted #CCC;text-align:left;">
                    	<a href="http://htgl.cnsn.gov.cn/website-webapp/wcm/initEdit_manuscript.action?websiteId=&manuscriptId=${m.manuscript_id }&status=4&channelId=${m.channel_id }" target="_blank">${m.title }</a>
                    </td>
                    <td width="10%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">
                    	${m.channel_name }
                    </td>
                    <td width="10%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">
                    	${m.published_time }
                    </td>
                    <td width="15%" style="color:#666; border-bottom:1px dotted #CCC; text-align:center;">
                    	${m.dept_name }
                    </td>
                    <td width="10%" style="color:#666; border-bottom:1px dotted #CCC;text-align:center;">
                    	<input type='button' name='delete' value='删除' onclick="doit('${m.manuscript_id }','${m.channel_id }',1);"/>
                    </td>
                </tr>
                </c:forEach>
                <tr align="center" height="30">
                    <td colspan="4">
                        <jsp:include page="common/page.jsp" >
                            <jsp:param value="${page.currentPage }" name="currentPage"/>
                            <jsp:param value="${page.total }" name="totalRecord"/>
                            <jsp:param value="10" name="pageSize"/>
                            <jsp:param value='myform' name="formName"/>
                        </jsp:include>

                    </td>
                </tr>
            </table>
            <input type="hidden" name="t" value="<%=(new java.util.Date()).getTime()%>"/>
            <input type="hidden" name="mid" id="mid"/>
            <input type="hidden" name="chid" id="chid"/>
            <input type="hidden" name="method" id="method"/>
        </form>

        <script language=javascript>
            function doit(k,m,j)
            {
                if(confirm("数据删除后不能恢复，您确定要删除？")){
                    document.getElementById("mid").value=k;
                    document.getElementById("chid").value=m;
                    document.getElementById("method").value=j;
                    document.getElementById("myform").submit();
                }
            }
        </script>

    </body>
</html>